package cn.edu.sctu.sctu_lysd.presenter;

import android.content.Context;
import android.widget.TextView;

import cn.edu.sctu.sctu_lysd.bean.AcceptBean;
import cn.edu.sctu.sctu_lysd.bean.JsonBaseBeen;
import cn.edu.sctu.sctu_lysd.bean.JsonBaseListBean;
import cn.edu.sctu.sctu_lysd.bean.ParcelData;
import cn.edu.sctu.sctu_lysd.biz.OrderDetailedMessageBiz;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.net.NetService;
import cn.edu.sctu.sctu_lysd.net.RetrofitHelper;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangfuxin on 2017-04-17.
 */

public class OrderDetailedPresenter implements OrderDetailedMessageBiz.Presenter {

    private Context mContext;
    private OrderDetailedMessageBiz.View mView;

    private NetService service;
    private SharePreferenceUtils sharePreferenceUtils;

    private Call<JsonBaseListBean<ParcelData>> parcelCall;
    private Call<JsonBaseBeen<AcceptBean>> acceptCall;

    public static final int NOT_DEPOSIT = 40000702;//未支付

    public OrderDetailedPresenter(Context mContext, OrderDetailedMessageBiz.View mView) {
        this.mContext = mContext;
        this.mView = mView;
    }

    @Override
    public void start() {

        RetrofitHelper helper = RetrofitHelper.getInstance();

        service = (NetService) helper.getService(NetService.class);

        sharePreferenceUtils = SharePreferenceUtils.getInstance(mContext);

    }

    @Override
    public void acceptCallCancel() {

        if (acceptCall != null)
            acceptCall.cancel();

    }

    @Override
    public void getOrder(long parcelId) {

        parcelCall = service.getOneParcel(parcelId);

        parcelCall.enqueue(new Callback<JsonBaseListBean<ParcelData>>() {
            @Override
            public void onResponse(Call<JsonBaseListBean<ParcelData>> call, Response<JsonBaseListBean<ParcelData>> response) {

                L.e("onResponse get One parcel", response.toString());

                if (response.body().getMeta().getCode() == 200) {

                    mView.getOrderSuccess(response.body().getData().get(0));
                }

            }

            @Override
            public void onFailure(Call<JsonBaseListBean<ParcelData>> call, Throwable t) {

                L.e("onResponse get One parcel erroe", t.getMessage());

                mView.getOrderFailed();

            }
        });

    }

    @Override
    public String getInputString(TextView view) {
        return null;
    }

    @Override
    public void accept(long id) {

        acceptCall = service.accept(sharePreferenceUtils.getString(CacheKeyConfig.CACHE_ACCESS_TOKEN), id);

        L.e("ididididid","-------" + id);

        acceptCall.enqueue(new Callback<JsonBaseBeen<AcceptBean>>() {
            @Override
            public void onResponse(Call<JsonBaseBeen<AcceptBean>> call, Response<JsonBaseBeen<AcceptBean>> response) {

                if (response.code() == 200){
                    switch (response.body().getMeta().getCode()) {
                        case 200:
                            mView.acceptSuccess();
                            break;
                        case NOT_DEPOSIT:
                            mView.notDeposit();
                            break;
                        default:
                            break;
                    }

                }else {
                    mView.acceptFailed();
                }



            }

            @Override
            public void onFailure(Call<JsonBaseBeen<AcceptBean>> call, Throwable t) {

                L.e("onResponse accept", t.getMessage());

                mView.acceptFailed();

            }
        });


    }
}
